package wikigearsconnector.paramdialog;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

public class ParametersDialog extends TitleAreaDialog {

	private Text login;

	public String getLogin() {
		return l;
	}

	public String getPassword() {
		return ps;
	}

	public int getPort() {
		return p;
	}

	public String getHost() {
		return h;
	}

	private Text password;
	private Spinner port;
	private Text host;

	private String l = "";
	private String ps = "";
	private String h = "";
	private int p = 0;

	public ParametersDialog(Shell parentShell) {
		super(parentShell);
		// setTitle("Connection parameters");
		// setMessage("Please insert parameters of connection");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Control p = super.createDialogArea(parent);
		Composite parentArea = new Composite((Composite) p, SWT.NONE);
		parentArea.setLayout(new GridLayout(2, false));
		parentArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label loginLabel = new Label(parentArea, SWT.NONE);
		loginLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		loginLabel.setText("Login");
		login = new Text(parentArea, SWT.NONE);
		login.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		login.setTextLimit(50);

		Label passLabel = new Label(parentArea, SWT.NONE);
		passLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		passLabel.setText("Password");
		password = new Text(parentArea, SWT.NONE);
		password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		password.setTextLimit(50);

		Label hostLabel = new Label(parentArea, SWT.NONE);
		hostLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		hostLabel.setText("Host");
		host = new Text(parentArea, SWT.NONE);
		host.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		host.setTextLimit(50);

		Label portLabel = new Label(parentArea, SWT.NONE);
		portLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		portLabel.setText("Port");
		port = new Spinner(parentArea, SWT.NONE);
		port.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		return parentArea;
	}

	public void init(String l, String ps, String h, String p) {
		this.l = l == null ? "" : l;
		this.ps = ps == null ? "" : ps;
		this.h = h == null ? "" : h;
		this.p = p == null || p.length() == 0 ? 8888 : Integer.parseInt(p);

		login.setText(this.l);
		password.setText(this.ps);
		host.setText(this.h);
		port.setValues(this.p, 0, 65535, 0, 1, 1);
	}


	@Override
	protected void okPressed() {
		l = login.getText();
		ps = password.getText();
		h = host.getText();
		p = port.getSelection();

		super.okPressed();
	}

}
